package cn.wolfcode.p2p.base.service.impl;
import java.util.Date;

import cn.wolfcode.p2p.base.domain.IpLog;
import cn.wolfcode.p2p.base.mapper.IpLogMapper;
import cn.wolfcode.p2p.base.query.IpLogQuery;
import cn.wolfcode.p2p.base.service.IIpLogService;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service@Transactional
public class IpLogServiceImpl implements IIpLogService {
        @Autowired
        private IpLogMapper ipLogMapper;

        @Override
        public PageResult query(IpLogQuery qo) {
                //先查出COunt,如果COUNT为0,返回空集合
                int row = ipLogMapper.selectCount(qo);
                if(row==0){
                        return new PageResult(Collections.EMPTY_LIST,0,1,qo.getPageSize());
                }
                List<IpLog> list = ipLogMapper.query(qo);

                return new PageResult(list,row,qo.getCurrentPage(),qo.getPageSize());
        }

        @Override
        public void save(String username, int state, int userType) {//新增了用户类型......
                IpLog ipLog = new IpLog();

                ipLog.setIp(UserContext.getIp());
                ipLog.setState(state);
                ipLog.setUsername(username);
                ipLog.setLoginTime(new Date());
                ipLog.setUserType(userType);
                ipLogMapper.insert(ipLog);

        }
}
